import { createStore } from 'vuex'
import { localStorageKeys, setLocalStore, removeLocalStore } from '@/utils/local-storage'
export default createStore({
  state: {
    logined: false,
    user: {
      id: '',
      name: ''
    },
    loginWindow: false,
    project: [],
    projectIds: [],
    projectOptions: []
  },
  mutations: {
    SET_PROJECT: (state, data) => {
      state.project = data
      state.projectIds = data.map(item => item.projectId)
      state.projectOptions = data.map(item => {
        return {
          value: item.projectId,
          title: item.projectName
        }
      })
    },
    TOOGLE_LOGIN_WINDOW: (state, opened) => {
      state.loginWindow = opened
    },
    SET_CURRENT_USER: (state, data) => {
      state.logined = true
      state.user = data
      state.loginWindow = false
    },
    USER_QUIT: (state) => {
      state.user = {
        id: '',
        name: ''
      }
      state.logined = false
    }
  },
  actions: {
    setCurrentProject ({ commit }, data) {
      commit('SET_PROJECT', data)
    },
    changeWindowLogin ({ commit }, data) {
      commit('TOOGLE_LOGIN_WINDOW', data)
    },
    setCurrentUser ({ commit }, data) {
      setLocalStore(localStorageKeys.USER_INFO, data)
      commit('SET_CURRENT_USER', data)
    },
    userQuit ({ commit }) {
      commit('USER_QUIT')
      removeLocalStore(localStorageKeys.USER_INFO)
    }
  },
  modules: {
  }
})
